﻿using ModelSerialize;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Threading;

namespace ModelSerializeTester
{
    class Program
    {
        static void Main(string[] args)
        {
            //long last_time = 1522829737, this_time = 1522829745;

            //using (var file = ModelSerializeHelper.GetInstance().MergeFile<TestModel>(last_time, this_time))
            //{
            //    Analysis(file.FileName);
            //}

            Write();

            MergeTest();

            while (true)
                Console.ReadLine();
        }

        private static void MergeTest()
        {
            Random random = new Random((int)DateTime.Now.Ticks);

            long last_time = ModelSerializeHelper.UnixTimestamp;

            new Thread(() =>
            {
                while (true)
                {
                    //Thread.Sleep(random.Next(5 * 1000, 15 * 1000));
                    Thread.Sleep(5 * 1000);

                    long this_time = ModelSerializeHelper.UnixTimestamp - random.Next(0, 2); ;

                    Console.WriteLine($"{last_time} {this_time}");


                    using (var file = ModelSerializeHelper.GetInstance().MergeFile<TestModel>(last_time, this_time))
                    {
                        var query_list = Analysis(file.FileName);
                        if (query_list != null)
                        {
                            last_time = query_list.Stop + 1;
                        }
                    }
                }
            })
            { IsBackground = true }.Start();
        }

        private static QueryList<TestModel> Analysis(string file)
        {
            QueryList<TestModel> query_list = null;

            using (FileStream fs = new FileStream(file, FileMode.Open))
            {
                query_list = ModelSerializeHelper<TestModel>.GetInstance().GetAllModelFromFile(fs);

                Console.WriteLine($"{query_list.Start} {query_list.Stop} {query_list.Items.Count}");
                foreach (var item in query_list.Items)
                {
                    Console.WriteLine(JsonConvert.SerializeObject(item));
                }
            }

            return query_list;
        }

        private static void Write()
        {
            //Console.WriteLine("Hello World!");

            Random random = new Random();

            Action action = () =>
            {
                while (true)
                {
                    var model = new TestModel
                    {
                        Name = ModelSerializeHelper.UnixTimestamp.ToString(),
                        Age = random.Next(1000, 1100),
                        AboutMe = "Hi, my name is JJJ" + ModelSerializeHelper.UnixTimestamp.ToString(),
                    };
                    ModelSerializeHelper<TestModel>.GetInstance().Add(model);

                    Thread.Sleep(random.Next(100, 300));
                }
            };

            new Thread(new ThreadStart(action)) { IsBackground = true }.Start();
        }
    }
}
